{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<span style=\"color: red;\">Requirement when running in Goolge Colab</span>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!pip install diffusers"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Z_NDNwoTudH8"
      },
      "source": [
        "# Chapter 1 - Sampling but not Inference\n",
        "\n",
        "In stable diffusion, the process of generating images is often called \"sampling\" rather than \"inference\". This term is used because the model doesn't simply deduce or infer a single correct output. Instead, it samples from a probability distribution, gradually transforming random noise into an image that matches the given prompt. Each generation can produce different results, much like taking multiple samples from a population. While \"inference\" is sometimes used in machine learning contexts, \"sampling\" more accurately describes the probabilistic and creative nature of image generation in stable diffusion models. On the other hand in context of diffusion models the steps taken to denoise the sample is usually referred to inference steps.\n",
        "\n",
        "In the simplest form of using sampling from Stable Diffusion, we can use the Diffusers library, a popular tool for working with diffusion models. By calling the pipeline, we initiate the internal sampling process, which then presents us with an image.\n",
        "\n",
        "In the next chapter, we will extract the internal elements of the pipeline. This will give us a better understanding of how the sampling process works internally, from generating noise to the gradual transformation that results in the final image."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hccUyIxfudH9"
      },
      "outputs": [],
      "source": [
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "from diffusers import StableDiffusionPipeline\n",
        "import torch\n",
        "\n",
        "\n",
        "model_id = \"stabilityai/stable-diffusion-2-1-base\"\n",
        "\n",
        "pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)\n",
        "pipe = pipe.to(\"cuda\")\n",
        "\n",
        "prompt = \"A photo of a woman, straight hair, light blonde and pink hair, smiling expression, grey background\"\n",
        "image = pipe(prompt).images[0]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "P_qB8dXFudH-"
      },
      "outputs": [],
      "source": [
        "image"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "L4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
